<template>
  <el-cascader
    :value="value"
    :options="options"
    :props="props"
    separator="-"
    @change="listChange"
  />
</template>

<script>
import { getDepartmentAPI } from '@/api/department'
import { transObjToTree } from '@/utils'

export default {
  name: 'SelectTree',
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      props: {
        label: 'name', // 要展示的字段
        value: 'id' // 要存储的字段
      },
      options: []
    }
  },
  created() {
    this.getDepartMent()
  },
  methods: {
    async getDepartMent() {
      this.options = transObjToTree(await getDepartmentAPI(), 0)
    },
    listChange(list) {
      if (list.length > 0) {
        this.$emit('input', list[list.length - 1])
      } else {
        this.$emit('input', null)
      }
    }

  }

}
</script>

<style scoped lang="scss">
</style>
